/**
 *******************************************************************************
 * @file  main.c
 * @brief Main program.
 @verbatim
   Change Logs:
   Date             Author          Notes
   2025-05-05       LKX             First version
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2022, Xiaohua Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by XHSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 *
 *******************************************************************************
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "main.h"
#include "sd.h"
#include "driver.h"
#include "AD7606B.h"
/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/
static uint8_t u8WriteBlocks[SD_CARD_BLK_SIZE * SD_CARD_BLK_NUM];
static uint8_t u8ReadBlocks[SD_CARD_BLK_SIZE * SD_CARD_BLK_NUM];
extern stc_sd_handle_t SdHandle;
/* Configures EIRQ. */
static void App_EIRQCfg(void);
/* Configures SDIOx. */
static void App_SDIOxCfg(void);
/* Configures USBFS. */
static void App_USBFSCfg(void);
/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/


/**
 * @brief  SD card erase.
 * @param  None
 * @retval int32_t:
 *           - LL_OK: SD card erase success
 *           - LL_ERR: SD card erase error
 */
int32_t SdCard_Erase(void)
{
    uint32_t i;
    int32_t i32Ret = LL_OK;

    /* Initialize read/write blocks */
    (void)memset(u8ReadBlocks, 0x20, (SD_CARD_BLK_SIZE * SD_CARD_BLK_NUM));
    /* Erase SD card */
    if (LL_OK != SD_Erase(&SdHandle, 0UL, SD_CARD_BLK_NUM))
    {
        i32Ret = LL_ERR;
    }

    /* Read SD card */
#if SD_TRANS_MD == SD_TRANS_MD_POLLING
    if (LL_OK != SD_ReadBlocks(&SdHandle, 0UL, SD_CARD_BLK_NUM, (uint8_t *)u8ReadBlocks, 2000UL))
    {
        i32Ret = LL_ERR;
    }
#elif SD_TRANS_MD == SD_TRANS_MD_INT
    u8RxCompleteFlag = 0U;
    if (LL_OK != SD_ReadBlocks_INT(&SdHandle, 0UL, SD_CARD_BLK_NUM, (uint8_t *)u8ReadBlocks))
    {
        i32Ret = LL_ERR;
    }
    /* Wait for transfer completed */
    while ((0U == u8RxCompleteFlag) && (0U == u8TxRxErrorFlag))
    {
    }
#else
    u8RxCompleteFlag = 0U;
    if (LL_OK != SD_ReadBlocks_DMA(&SdHandle, 0UL, SD_CARD_BLK_NUM, (uint8_t *)u8ReadBlocks))
    {
        i32Ret = LL_ERR;
    }
    /* Wait for transfer completed */
    while ((0U == u8RxCompleteFlag) && (0U == u8TxRxErrorFlag))
    {
    }
#endif

    /* Check whether data value is OxFF or 0x00 after erase SD card */
    for (i = 0UL; i < (SD_CARD_BLK_SIZE * SD_CARD_BLK_NUM); i++)
    {
        if ((0x00U != u8ReadBlocks[i]) && (0xFFU != u8ReadBlocks[i]))
        {
            i32Ret = LL_ERR;
            break;
        }
    }
#if SD_TRANS_MD != SD_TRANS_MD_POLLING
    if (0U != u8TxRxErrorFlag)
    {
        i32Ret = LL_ERR;
    }
#endif
    if (LL_OK != i32Ret)
    {
        DDL_Printf("SD card erase failed!\r\n");
    }

    return i32Ret;
}

/**
 * @brief  SD card multi-block read/write.
 * @param  None
 * @retval int32_t:
 *           - LL_OK: SD card multi-block read/write success
 *           - LL_ERR: SD card multi-block read/write error
 */
int32_t SdCard_RdWrMultiBlock(void)
{
    int32_t i32Ret = LL_OK;

    /* Initialize read/write blocks */
    (void)memset(u8WriteBlocks, 0x20, (SD_CARD_BLK_SIZE * SD_CARD_BLK_NUM));
    (void)memset(u8ReadBlocks, 0, (SD_CARD_BLK_SIZE * SD_CARD_BLK_NUM));
    /* Write SD card */
#if SD_TRANS_MD == SD_TRANS_MD_POLLING
    if (LL_OK != SD_WriteBlocks(&SdHandle, 0UL, SD_CARD_BLK_NUM, (uint8_t *)u8WriteBlocks, 2000U))
    {
        i32Ret = LL_ERR;
    }
#elif SD_TRANS_MD == SD_TRANS_MD_INT
    u8TxCompleteFlag = 0U;
    if (LL_OK != SD_WriteBlocks_INT(&SdHandle, 0UL, SD_CARD_BLK_NUM, (uint8_t *)u8WriteBlocks))
    {
        i32Ret = LL_ERR;
    }
    /* Wait for transfer completed */
    while ((0U == u8TxCompleteFlag) && (0U == u8TxRxErrorFlag))
    {
    }
#else
    u8TxCompleteFlag = 0U;
    if (LL_OK != SD_WriteBlocks_DMA(&SdHandle, 0UL, SD_CARD_BLK_NUM, (uint8_t *)u8WriteBlocks))
    {
        i32Ret = LL_ERR;
    }
    /* Wait for transfer completed */
    while ((0U == u8TxCompleteFlag) && (0U == u8TxRxErrorFlag))
    {
    }
#endif

    /* Read SD card */
#if SD_TRANS_MD == SD_TRANS_MD_POLLING
    if (LL_OK != SD_ReadBlocks(&SdHandle, 0UL, SD_CARD_BLK_NUM, (uint8_t *)u8ReadBlocks, 2000UL))
    {
        i32Ret = LL_ERR;
    }
#elif SD_TRANS_MD == SD_TRANS_MD_INT
    u8RxCompleteFlag = 0U;
    if (LL_OK != SD_ReadBlocks_INT(&SdHandle, 0UL, SD_CARD_BLK_NUM, (uint8_t *)u8ReadBlocks))
    {
        i32Ret = LL_ERR;
    }
    /* Wait for transfer completed */
    while ((0U == u8RxCompleteFlag) && (0U == u8TxRxErrorFlag))
    {
    }
#else
    u8RxCompleteFlag = 0U;
    if (LL_OK != SD_ReadBlocks_DMA(&SdHandle, 0UL, SD_CARD_BLK_NUM, (uint8_t *)u8ReadBlocks))
    {
        i32Ret = LL_ERR;
    }
    /* Wait for transfer completed */
    while ((0U == u8RxCompleteFlag) && (0U == u8TxRxErrorFlag))
    {
    }
#endif

    /* Check data value */
    if (0 != memcmp(u8WriteBlocks, u8ReadBlocks, (SD_CARD_BLK_SIZE * SD_CARD_BLK_NUM)))
    {
        i32Ret = LL_ERR;
    }
#if SD_TRANS_MD != SD_TRANS_MD_POLLING
    if (0U != u8TxRxErrorFlag)
    {
        i32Ret = LL_ERR;
    }
#endif
    if (LL_OK != i32Ret)
    {
        DDL_Printf("SD card multi-block read/write failed!\r\n");
    }

    return i32Ret;
}

/**
 * @brief  Main function of the project
 * @param  None
 * @retval int32_t return value, if needed
 */
int16_t adc_data[8]; // 存储8通道原始数据
float voltages[8];   // 存储转换后的电压值
extern int wave_task(void);

 int i32EraseRet = 0, i32MultiBlockRet;
int32_t main(void)
{       

   

    Driver_Init();
//    i32EraseRet = SdCard_Erase();
//    i32MultiBlockRet = SdCard_RdWrMultiBlock();
//    stc_sd_card_csd_t pstcCardCSD = {0};
//    SD_GetCardCSD(&SdHandle, &pstcCardCSD);

   wave_task();
    for (;;)
    {
        
      
    }
}

// SDIOx Config
static void App_SDIOxCfg(void)
{
}

/**
 * @}
 */

/**
 * @}
 */

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
